import urllib.request
import urllib.parse

start_url='https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery1123004069909820181361_1710938112242&sortColumns=UPDATE_DATE%2CSECURITY_CODE&sortTypes=-1%2C-1&pageSize=50&'
end_url='&reportName=RPT_LICO_FN_CPD&columns=ALL&filter=(REPORTDATE%3D%272023-12-31%27)'

headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0'
}


def create_request(page):
    data={
        'pageNumber':page
    }
    url=start_url+urllib.parse.urlencode(data)+end_url
    # url='https://quote.eastmoney.com/center/gridlist.html#newshares'
    request=urllib.request.Request(url=url,headers=headers)
    return request


import join
import jsonpath
import json
def get_content(request):
    response=urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    # 利用split切割在获取索引，去除掉不想要的部分
    return content
def download(content,page):
    with open('年报'+str(page)+'.json','w',encoding='utf-8')as fp:
        fp.write(content)
def path(page):
    obj = json.load(open('年报'+str(page)+'.json','r',encoding='utf-8'))
    name_list=jsonpath,jsonpath(obj,'$..SECURITY_NAME_ABBR')
    print(name_list)

#入口
if __name__ == '__main__':
    for page in range(1,10):
        # 请求对象的定制
        # request = create_request(page)
        # 获取响应数据
        # content = get_content(request)
        # 保存到本地
        # download(content,page)
        # 解析
        path(page)

import json
import jsonpath
for page in range(1,10):
    obj = json.load(open('年报'+str(page)+'.json','r',encoding='utf-8'))
    name_list = jsonpath.jsonpath(obj,"$..SECURITY_NAME_ABBR")      #名称
    BASIC_EPS_list = jsonpath.jsonpath(obj,"$..BASIC_EPS")               #每股收益
    TOTAL_OPERATE_INCOME_list = jsonpath.jsonpath(obj,"$..TOTAL_OPERATE_INCOME")    #营业总收入
    YSTZ_list = jsonpath.jsonpath(obj,"$..YSTZ")                    #营业总收入同比增长
    YSHZ_list = jsonpath.jsonpath(obj,"$..YSHZ")                    #季度环比增长
    PARENT_NETPROFIT_list = jsonpath.jsonpath(obj,"$..PARENT_NETPROFIT")        #净利润
    SJLTZ_list = jsonpath.jsonpath(obj,"$..SJLTZ")                    #同比增长
    SJLHZ_list = jsonpath.jsonpath(obj,"$..SJLHZ")                   #季度环比增长
    BPS_list = jsonpath.jsonpath(obj,"$..BPS")                     #每股净资产
    WEIGHTAVG_ROE_list = jsonpath.jsonpath(obj,"$..WEIGHTAVG_ROE")          #净资产收益率
    NOTICE_DATE_list = jsonpath.jsonpath(obj,"$..NOTICE_DATE")            #公告日期
    for i in range(0,50):
            print(name_list[i] + str(BASIC_EPS_list[i]) + str(TOTAL_OPERATE_INCOME_list[i]) + str(YSTZ_list[i]) + str(YSHZ_list[i]) +
                  str(PARENT_NETPROFIT_list[i]) + str(SJLTZ_list[i]) + str(SJLHZ_list[i]) + str(BPS_list[i]) + str(WEIGHTAVG_ROE_list[i]) +
                  str(NOTICE_DATE_list[i]))